﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace KTX
{
    public partial class frmThemHopDong : DevExpress.XtraEditors.XtraForm
    {
        public frmThemHopDong()
        {
            InitializeComponent();
        }
        Data.HopDong _hd;
        Data.QLKTXEntities db = new Data.QLKTXEntities();
        public frmThemHopDong(Data.HopDong hd):this()
        {
            _hd = hd;
        }

        private void frmThemHopDong_Load(object sender, EventArgs e)
        {
            doiTuongBindingSource.DataSource = db.DoiTuong;
            lopBindingSource.DataSource = db.Lop;
            phongBindingSource.DataSource = db.Phong;
            hopDongBindingSource.DataSource = db.HopDong;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            var HP = (Data.HopDong)hopDongBindingSource.Current;
            HP.SinhVien = new Data.SinhVien()
            {
                Lop= db.Lop.First(),
                DoiTuong=db.DoiTuong.First(),
                MaSV=maSVTextEdit.EditValue.ToString()
            };
        }

        private void barThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.barLuu.PerformClick();
            hopDongBindingSource.AddNew();
        }

        private void barXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            hopDongBindingSource.RemoveCurrent();
        }

        private void barLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            db.SaveChanges(System.Data.Objects.SaveOptions.None);
        }

        private void barIn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.hopDongBindingSource.Current != null)
            {
                var report = new BaoCao.xHopDong();
                BindingSource bs = (BindingSource)report.DataSource;
                bs.DataSource = hopDongBindingSource.Current;
                report.ShowPreviewDialog();
            }
        }

        private void hopDongBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new Data.HopDong()
            {
                NamHoc= DateTime.Now.Year,
                NgayBD= DateTime.Now,
                SoThang=12,
                ThangGiaHan=0
            };
        }

        private void frmThemHopDong_Shown(object sender, EventArgs e)
        {
            if (_hd!=null)
            {
                hopDongBindingSource.DataSource = _hd;
            }
            else
            {
                barThem.PerformClick();
            }
        }

        private void barDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
    }
}